<?php
//--------------------------------------------------------------------------------------------------------
    require_once($config['app_dir'] . "/includes/db.class.php");
    //----------------------------------------------------------------------------------------------------
     abstract class objDataList {
            public $cache_time=0;
            protected $db = false;
            protected $_sql_querys = array();
            protected $data = false;
            protected $prepared = false;

        public function __construct() {
            $this->db = objDataBase::GetInstance();
            $this->_sqls();
        } //constructor

        public function isEmpty() {
            $this->Prepare();
            return false===$this->data || empty($this->data);
        } //isEmpty

        public function GetAssocArr($force=false){
            $this->Prepare($force);
            return $this->data;
        } //GetAssocArr

        abstract protected function Prepare($force=false);

        abstract protected function _sqls();

    }//class DataList
    //----------------------------------------------------------------------------------------------------
    class objPairs extends objDataList {

        protected function Prepare($force=false) {
            if( !$force && $this->prepared ) return $this->data;

            try {
                $rs = $this->db->handler->CacheExecute($this->cache_time, $this->_sql_querys['pairs']);
                if( $rs ) {
                    $this->data = array();
                    while( !$rs->EOF ) {
                        $this->data[]= array(
                                'Pair' => $rs->fields['Pair'],
                                'TradeID' => $rs->fields['TradeID'],
                                'pUSD' => $rs->fields['PriceUSD'],
                                'defSpread' => $rs->fields['DefaultSpread'],
                                'pipSize' => $rs->fields['PipSize']
                                );
                        $rs->MoveNext();
                    }//while
                }//if

            } catch(exception $e) {
                if (true == $this->db->debug ) {
                    adodb_pr($e);
                    $e = adodb_backtrace($e->trace);
                }
                die('DB Error: '.$e->sql);
            }
            $this->prepared=true;
            return $this->data;
        } //Prepare

        protected function _sqls(){
            $this->_sql_querys['pairs'] = "SELECT * FROM fa_trades ORDER BY TradeID asc";
        }//_sqls

    } //class Pairs
    //----------------------------------------------------------------------------------------------------
    class objSystems extends objDataList{

        protected function Prepare($force=false) {
            if( !$force && $this->prepared ) return $this->data;

            try {
                $rs = $this->db->handler->CacheExecute($this->cache_time, $this->_sql_querys['systems']);
                if( $rs ) {
                    $this->data = array();
                    while( !$rs->EOF ) {
                        $this->data[]= array(
                                'SystemID' => $rs->fields['SystemID'],
                                'Paused' => $rs->fields['Paused'],
                                'Name' => $rs->fields['Name']
                                );
                        $rs->MoveNext();
                    }//while
                }//if

            } catch(exception $e) {
                if (true == $this->db->debug ) {
                    adodb_pr($e);
                    $e = adodb_backtrace($e->trace);
                }
                die('DB Error: '.$e->sql);
            }
            $this->prepared=true;
            return $this->data;
        } //Prepare

        public function GetFirst() {
            $this->data = $this->db->handler->CacheGetRow($this->cache_time, $this->_sql_querys['systems']);
            $result = false;
            if( $this->data ) {
                $result = $this->data;
                $this->data = array(0 => $this->data);
                $this->prepared=true;
            }
            return $result;
        }//getFirst

        public function GetAssocArrByID($force=false){
            $this->Prepare($force);
            $result = array();
            foreach($this->data as $item)
                $result[$item['SystemID']] = $item;
            return $result;
        } //GetAssocArrByID

        protected function _sqls(){
            $this->_sql_querys['systems'] = "SELECT * FROM fa_systems WHERE Active=1 ORDER BY SystemID asc";
        }//_sqls

    } //class Systems
    //----------------------------------------------------------------------------------------------------
    class objPairsSystems extends objDataList {
            protected $system_id = false;

            public $order_by_rate = true;
            public $order_by_sysname = false;

        public function __set($name, $value) {
            switch( strtolower($name) ) {
                case 'system':
                case 'systemid':
                case 'system_id':
                    $this->system_id = intval($value);
                    $this->_sqls();
                break;
            }//switch
        } //set

        public function __get($name) {
            $result = false;
            switch( strtolower($name) ) {
                case 'system':
                case 'systemid':
                case 'system_id':
                    $result =  $this->system_id;
                break;
            }//switch
            return $result;
        } //get

        protected function Prepare($force=false) {
            if( !$force && $this->prepared ) return $this->data;
            try {
                $this->_sqls();
                $rs = $this->db->handler->CacheExecute($this->cache_time, $this->_sql_querys['list']);
                if( $rs ) {
                    $this->data = array();
                    while( !$rs->EOF ) {
                        $this->data[]= array(
                                'TradeID' => $rs->fields['TradeID'],
                                'Pair' => $rs->fields['Pair'],
                                'Paused' => $rs->fields['Paused'],
                                'PriceUSD' => $rs->fields['PriceUSD'],
                                'DefaultSpread' => $rs->fields['DefaultSpread'],
                                'PipSize' => $rs->fields['PipSize'],
                                'CurrID1' => $rs->fields['CurrID1'],
                                'CurrID2' => $rs->fields['CurrID2'],
                                'SystemID' => $rs->fields['SystemID'],
                                'SysName' => $rs->fields['SysName'],
                                'SysShortName' => $rs->fields['SysShortName'],
                                'TimeFrame' => $rs->fields['TimeFrame'],
                                'Description' => $rs->fields['Description']
                                );
                        $rs->MoveNext();
                    }//while
                }//if

            } catch(exception $e) {
                if (true == $this->db->debug ) {
                    adodb_pr($e);
                    $e = adodb_backtrace($e->trace);
                }
                die('DB Error: '.$e->sql);
            }
            $this->prepared = true;
            return $this->data;
        } //Prepare

        protected function _sqls() {
            $where_system = '';
            if( $this->system_id ) $where_system = ' ts.SystemID = ' . $this->system_id;
            //----------------------------------------------------------------------------
            $order = array();
            if( $this->order_by_rate ) $order[] = ' r.SortIndex ';
            if( $this->order_by_sysname ) $order[] = ' ts.SysName DESC';
            $order = implode(',', $order);
            if( $order ) $order = " ORDER BY  $order";
            //----------------------------------------------------------------------------
            $this->_sql_querys['list'] =
                " SELECT " .
                  " t.TradeID, " .
                  " t.Pair, " .
                  " COALESCE(ts.Paused, 0) AS Paused, " .
                  " t.PriceUSD, " .
                  " t.DefaultSpread, " .
                  " t.PipSize, " .
                  " t.CurrID1, " .
                  " t.CurrID2, " .
                  " COALESCE(ts.SystemID, 0) AS SystemID, " .
                  " COALESCE(ts.SysName ,'Default System') as SysName, " .
                  " COALESCE(ts.SysShortName ,'Default System') as SysShortName, " .
                  " COALESCE(ts.TimeFrame, 240) as TimeFrame, " .
                  " ts.Description " .
                " FROM " .
                    " fa_trades t " .
                        " LEFT OUTER JOIN   fa_rates r ON ( r.TradeID = t.TradeID ) " .
                        " LEFT OUTER JOIN  " .
                            " ( " .
                                " SELECT  " .
                                  " sub_ts.TradeID, " .
                                  " sub_s.SystemID, " .
                                  " sub_s.Paused, " .
                                  " sub_s.Name as SysName, " .
                                  " sub_s.ShortName as SysShortName, " .
                                  " sub_s.TimeFrame, " .
                                  " sub_s.Description " .
                                " FROM " .
                                    " fa_trades_systems sub_ts  " .
                                        " LEFT OUTER JOIN fa_systems sub_s ON (sub_ts.SystemID = sub_s.SystemID)   " .
                                " WHERE sub_s.Active=1 " .
                            " ) ts " .
                        " ON (ts.TradeID = t.TradeID)	  ".
                " WHERE (1=1) "  .
                ($where_system ? " AND $where_system " : '' ) .
                $order;
        }//_sqls

    }//PairsSystems
    //----------------------------------------------------------------------------------------------------
    class objMailsNotSend extends objDataList {
            protected $_max_tries = 10;
            protected $_mailer_session = false;

        public function __set($name, $value) {
            switch( strtolower($name) ) {
                case 'maxtries':
                    $this->_max_tries = intval($value);
                    $this->_sqls();
                break;
                //-----------------------------------------------------------------------------------------
                case 'mailersession':
                    $this->_mailer_session = intval($value);
                    $this->_sqls();
                break;
            }//switch
        } //set

        public function __get($name) {
            $result = false;
            switch( strtolower($name) ) {
                case 'maxtries':
                    $result =  $this->_max_tries;
                break;
                //-----------------------------------------------------------------------------------------
                case 'mailersession':
                    $result = $this->_mailer_session;
                break;
            }//switch
            return $result;
        } //get

        public function findMaxTries() {
            return $this->db->handler->getOne($this->_sql_querys['max_tries']);
        } //foundMaxTries

        protected function Prepare($force=false) {
            if( !$force && $this->prepared ) return $this->data;

            try {
                $rs = $this->db->handler->CacheExecute($this->cache_time, $this->_sql_querys['mails']);
                if( $rs ) {
                    $this->data = array();
                    while( !$rs->EOF ) {
                        $this->data[]= array(
                                'MailID' => $rs->fields['MailID']
                                );
                        $rs->MoveNext();
                    }//while
                }//if

            } catch(exception $e) {
                if (true == $this->db->debug ) {
                    adodb_pr($e);
                    $e = adodb_backtrace($e->trace);
                }
                die('DB Error: '.$e->sql);
            }
            $this->prepared=true;
            return $this->data;
        } //Prepare

        protected function _sqls() {
            $this->_sql_querys['mails'] =
                " SELECT m.MailID ".
                " FROM fa_mailer_mails m ".
                " WHERE COALESCE(m.StatusID,0)>0 and m.Tries<=$this->_max_tries ".
                ( $this->_mailer_session ?' AND ( SessionID='.$this->_mailer_session.' ) ' : ' ' );
            //-------------------------------------------------------------------------------------------
            $this->_sql_querys['max_tries'] =
                " SELECT max(m.Tries) ".
                " FROM fa_mailer_mails m ".
                " WHERE COALESCE(m.StatusID,0)>0  ".
                ( $this->_mailer_session ?' AND ( SessionID='.$this->_mailer_session.' ) ' : ' ' );
        }//_sqls

    } //class MailsNotSend

//--------------------------------------------------------------------------------------------------------
?>
